//
//  KCHttpCenter.m
//  Jade
//
//  Created by king on 16/6/24.
//  Copyright © 2016年 KC. All rights reserved.
//

#import "KCHttpCenter.h"
#import "KCHttpRequest.h"
#import "KCStation.h"
#import "KCCargo.h"

@interface KCHttpCenter()<KCStationDelegate>{

    KCStation *_station;

}

@end

@implementation KCHttpCenter

- (id)init {
    if (self = [super init]) {
        _station = [KCStation RegisterStation:KHttpStation delegate:self];
    }
    return self;
}

- (id)dispatch:(KCCargo *)cargo {
    
    NSDictionary *params        = cargo.cargoObj;
    NSString *url               = params[KC_URL];
    NSMutableDictionary *data   = [NSMutableDictionary dictionaryWithDictionary:params[KC_PARAMS_DATA]];
    [data setValue:APP_VERSION forKey:@"version"];

    switch (cargo.destinationID) {
        case KHttpStation_Get: {
            [self get:url :data :cargo];
            break;
        }
        case KHttpStation_Post: {
            [self post:url :data :cargo];
            break;
        }
        case KHttpStation_UploadImage: {
            [self uploadImage:url :data :params[KC_UPLOAD_DATA] :cargo];
            break;
        }
        default:
            break;
    }
    return nil;
}



- (void)get:(NSString *)url :(NSDictionary *)data :(KCCargo *)cargo {
    
    [KCHttpRequest get:url :^(id response) {
        [_station sendCargo:cargo.originID cargoID:cargo.cargoID cargoObj:response];
    }];
}

- (void)post:(NSString *)url :(NSDictionary *)data :(KCCargo *)cargo {
 
    [KCHttpRequest post:url :data :^(id response) {
        [_station sendCargo:cargo.originID cargoID:cargo.cargoID cargoObj:response];
    }];
    
}

- (void)uploadImage:(NSString *)url :(NSDictionary *)data :(NSArray *)imageDataArray :(KCCargo *)cargo {
    
    [KCHttpRequest uploadImage:url :data :imageDataArray :^(id response) {
        [_station sendCargo:cargo.originID cargoID:cargo.cargoID cargoObj:response];
    }];
}


@end
